// pages/pay/card/index.js
const app = getApp()
import http from '../../../utils/request.js';
import alert from '../../../utils/alert.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
      orderNo:"",
      chaFee:0,
      cardPopupvisible:false,
      cardPopupTitle:'123456',
      myCards:[],
      cardInputIndex:'',
      card:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderNo: options.order_no,
    });
    this.getMyCardList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getMyCardList:function(e){
    http.post(app.config.service.getMyCard, { app_id: app.globalData.app_id }).then(data => {
      var myCards=[];
      data.forEach((value, index) => {
        if (value.is_vip == "1") {
          var vipText = "会员卡"
        } else {
          var vipText = "储值卡"
        }
        value['key'] = vipText+"："+ value.card_no + '（' + value.money+')'
        myCards.push(value);
      })

      this.setData({
        myCards: myCards,
        cardInputIndex: myCards.length
      })
    }, err => {
      alert.alert(err)
    });
  },
  bindCardChange:function(e){
    console.log(e);
    var myCards=this.data.myCards;
    console.log(myCards[e.detail.value]);
    this.setData({
      card: myCards[e.detail.value],
      cardPopupTitle: myCards[e.detail.value].card_no,
      cardPopupvisible: true
    })
  },
  formSubmit: function (e) {
    var that = this;
    if (e.detail.value.amount>that.data.card.money) {
      alert.alert('支付额度不能超过' + that.data.card.money);
    }else{
      var data = Object.assign({app_id:app.globalData.app_id, order_no: that.data.orderNo, pay_type: 'store', pay_channel: 'card' }, e.detail.value);
      http.post(app.config.service.goOrderPay, data).then(data => {
        wx.showToast({
          title: '支付成功'+data.amount+'元',
        })
        wx.navigateBack()
      }, err => {
        alert.alert(err)
      });
    }

  },
  inputCard: function(e){
    var that = this;
    var urlOptions = {
      url: config.service.getCardInfo,
      data: {card_no:e.detail.value},
      method: 'POST',
      success(result) {
        if (result.data.code) {
          that.setData({
            card:result.data.data,
            cardPopupTitle:result.data.data.card_no,
            cardPopupvisible:true
          });
        } else {
          util.showModel('支付错误', result.data.msg);
        }
      },
      fail(error) {
        util.showModel('请求失败', error);
        console.log('request fail', error);
      }
    };
    qcloud.request(urlOptions);
  },
  onCardClose:function(e){
    this.setData({
      card_no: '',
      card: {},
      cardPopupvisible: false
    });
  },
  useCard:function(e){
    if(this.data.card.money<=0){
      this.setData({
        cardPopupvisible: false,
        card: {}
      });
      alert.alert('余额不足无法用于支付')
    }else{
      this.setData({
        cardPopupvisible: false,
      });
    }

  }
});